require([
    "esri/Map",
    "esri/views/MapView",
    "esri/Basemap",
    "esri/geometry/Extent",
    "esri/geometry/Point",
    "esri/layers/TileLayer",
    "esri/layers/FeatureLayer",
    "esri/extlayers/DivLayer",
    "dojo/dom-construct",
    "dojo/domReady!"
], function(Map, MapView,Basemap,Extent,Point,TileLayer,FeatureLayer,DivLayer,domConstruct) {
    //http://ypgis.cindata.cn/arcgis/rest/services/china/MapServer
    //http://ypgis.cindata.cn/arcgis/rest/services/qd/MapServer
    console.log($(document.body).height());
    $("#viewDiv").height($(document).height()-80);
    var basemapLayer = new TileLayer({
        url: "http://ypgis.cindata.cn/arcgis/rest/services/china/MapServer"
    });
    var customBasemap = new Basemap({
        baseLayers: [basemapLayer],
        title: "底图",
        id: "myBasemap"
    });

    var map = new Map({
        basemap: customBasemap
    });
    var view = new MapView({
        container: "viewDiv",
        map: map
    });
    view.extent = new Extent({
        xmin: 1.3403422817964608E7,
        ymin: 4313542.681911737,
        xmax: 1.3411508500802642E7,
        ymax: 4318051.190928755,
        spatialReference: {
            wkid: 102100
        }
    });
    var plateRenderer = {
        type: "simple",  // autocasts as new SimpleRenderer()
        symbol: {
            type: "simple-fill",  // autocasts as new SimpleFillSymbol()
            color: [ 255,255,88, 0.2],
            style: "solid",
            outline: {  // autocasts as new SimpleLineSymbol()
                color: "red",
                width: 1
            }
        }
    };
    var fl2 = new FeatureLayer({
        url: "http://ypgis.cindata.cn/arcgis/rest/services/qd/MapServer/1",
        renderer:plateRenderer,
        minScale:35000,
        maxScale:0
    });
    map.add(fl2);
    var comRenderer = {
        type: "simple",  // autocasts as new SimpleRenderer()
        symbol: {
            type: "picture-marker",  // autocasts as new SimpleMarkerSymbol()
            url: "/assets/com.png",
            width: "32px",
            height: "32px"
        }
    };
    var fl = new FeatureLayer({
        url: "http://ypgis.cindata.cn/arcgis/rest/services/qd/MapServer/0",
        renderer:comRenderer,
        minScale:35000,
        maxScale:0,
        outFields:["*"]
    });
    map.add(fl);

    var divLayer = new DivLayer({ direction:'top-mid',minScale:35000,maxScale:0});
    map.add(divLayer);

    fl.queryFeatures().then(function(results){
        $.each(results.features,function(i,feature){
            var priceNode=domConstruct.create('div', {class: 'price'});
            var standardpr=feature.attributes.standardpr;
            if(!standardpr){standardpr=0.00}
            priceNode.append("评估价 "+standardpr+" 元/平方米");
            divLayer.add({
                html:priceNode ,
                geometry: feature.geometry
            });
        });
    });
    $("#evaluate").click(function () {
        var cid=$("#communityId").val();
        if(cid){
            var queryParams = fl.createQuery();
            queryParams.where =queryParams.where+ " AND ccode="+cid+"";
            fl.queryFeatures(queryParams).then(function(results){
                if(results.features.length>0){
                    view.goTo({
                        'center':results.features[0].geometry,
                        'scale':16
                    });
                }else{
                    swal("提示", "未查询到具体的地理位置!", "warning");
                }

            });
        }else{
            swal("提示", "请先检索小区!", "warning");
        }
    });
});

$("#codeAddress").change(function () {
    $("#searchCommunity").click();
});
$("#searchCommunity").click(function () {
    var codeAddress=$("#codeAddress").val();
    var keyword=$("#keyword").val();
    $.ajax({
        url: "/valuationHistoryForSSQ/searchCommunity",
        type: "GET",
        data: {'codeAddress': codeAddress,'keyword':keyword} ,
        success: function(response){
            if(response.result.length>0){
                var res=response.result[0];
                $("#communityName").val(res[0]);
                $("#plateName").val(res[1]);
                $("#houseAddress").val(res[2]);
                $("#currentFloor").val(res[3]+"/"+res[4]);
                $("#buildYear").val(res[5]);
                $("#communityId").val(res[6]);

            }else{
                $("#communityId").val("");
                $("#communityName").val("");
                $("#plateName").val("");
                $("#houseAddress").val("");
                $("#currentFloor").val("");
                $("#buildYear").val("");
            }
        }
    });
});